package com.tongtong.stsuserapi.service.base.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tongtong.stscommon.enumration.Message;
import com.tongtong.stscommon.enumration.Status;
import com.tongtong.stscommon.exception.STSResourcesOperationException;
import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.message.STSSuccessMessage;
import com.tongtong.stscommon.utils.checker.ObjectChecker;
import com.tongtong.stsuserapi.dao.NotificationDao;
import com.tongtong.stsuserapi.pojo.Notification;
import com.tongtong.stsuserapi.service.base.NotificationService;
import io.netty.handler.codec.MessageAggregationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

@Service
public class NotificationServiceImpl implements NotificationService {

	@Autowired
	NotificationDao notificationDao;

	@Override
	public STSMessage getUnreadNotification(Long userid) {

		LambdaQueryWrapper<Notification> lqw = new LambdaQueryWrapper<>();
		lqw.isNull(Notification::getUpdateTime);
		lqw.eq(Notification::getRecipientId,userid);
		lqw.orderByDesc(Notification::getSendTime);
		List<Notification> list = notificationDao.selectList(lqw);
		Long len = notificationDao.selectCount(lqw);

		HashMap<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("len",len);

		return STSSuccessMessage.from(map);
	}

	@Override
	public STSMessage getReadNotification(Long userid) {
		LambdaQueryWrapper<Notification> lqw = new LambdaQueryWrapper<>();
		lqw.isNotNull(Notification::getUpdateTime);
		lqw.eq(Notification::getRecipientId,userid);
		lqw.orderByDesc(Notification::getUpdateTime);
		List<Notification> list = notificationDao.selectList(lqw);
		Long len = notificationDao.selectCount(lqw);

		HashMap<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("len",len);

		return STSSuccessMessage.from(map);
	}

	@Override
	public STSMessage getSendNotification(Long userid) {
		LambdaQueryWrapper<Notification> lqw = new LambdaQueryWrapper<>();
		lqw.eq(Notification::getSenderId,userid);
		lqw.orderByDesc(Notification::getSendTime);
		List<Notification> list = notificationDao.selectList(lqw);
		Long len = notificationDao.selectCount(lqw);

		HashMap<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("len",len);

		return STSSuccessMessage.from(map);
	}

	@Override
	public STSMessage readNotification(Long userid, Long notificationId) {

		Notification notification = notificationDao.selectById(notificationId);
		ObjectChecker.ObjectsFromDBNullCheck(notification);

		// 检查是否有权限读取消息
		if(!notification.getRecipientId().equals(userid)) {
			throw new STSResourcesOperationException(Status.AccessResourcesError,Message.AccessResourcesError);
		}

		// 设置更新时间，标记为已读
		notification.setUpdateTime(new Timestamp(System.currentTimeMillis()));

		try	{
			notificationDao.updateById(notification);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.UpdateResourcesError, e.getMessage());
		}

		return STSSuccessMessage.from(notification);
	}

	public STSMessage deleteNotification(Long userid, Long notificationId) {

		Notification notification = notificationDao.selectById(notificationId);
		ObjectChecker.ObjectsFromDBNullCheck(notification);

		// 检查是否有权限删除消息
		if(!notification.getRecipientId().equals(userid)) {
			throw new STSResourcesOperationException(Status.AccessResourcesError,Message.AccessResourcesError);
		}

		try	{
			notificationDao.deleteById(notification.getId());
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.DeleteResourcesError, e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK, Message.OK);
	}

	@Override
	public STSMessage sendNotification(Long senderId, Long recipientId, String content, Integer noticeType) {

		Notification notification = new Notification();
		notification.setSendTime(new Timestamp(System.currentTimeMillis()));
		notification.setSenderId(senderId);
		notification.setRecipientId(recipientId);
		notification.setType(noticeType);
		notification.setContent(content);

		try	{
			notificationDao.insert(notification);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.AddResourcesError, e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK, Message.OK);
	}
}
